package com.winit.openapi.wop.inbound.validator;

import java.util.Arrays;
import java.util.Date;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.inbound.vo.InboundEnumConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.util.DateUtil;
import com.winit.openapi.util.ValidateUtil;

/**
 * 海外仓入库单 提货验货 校验器类
 * 
 * @version <pre>
 * Author	  Version		Date		Changes
 * dinghua.an	1.0  		2015年11月10日 	Created
 *
 * </pre>
 * @since 1.
 */
public class InboundPickupFieldValidator {
	
	private static String[] PICKUP_DATE_TIMES = new String[] {
			"09:00:00-12:00:00", "14:00:00-16:00:00", "15:00:00-17:00:00","16:00:00-18:00:00" };

    /**
     * 验货/提货信息 下一步
     * 
     * @param json
     */
    public static void ValidInspectionPickUpByNextField(JSONObject json) {
        // 入库单
        ValidateUtil.validNotNull(json, "orderNo");
        // 验货方式
        ValidateUtil.validNotNull(json, "inspectionType");
        validInspectionType(json, "inspectionType");

        if (json.getString("inspectionType").equals(InboundEnumConstant.InspectionTypeStatus.VISIT_INSPECTION.getVal())) {
            ValidateUtil.validNotNull(json, "date");
            Object dateStr = json.get("date");
            validDate(dateStr, "date");
            
            // 验货地址
            ValidateUtil.validNotNull(json, "addressCode");
            ValidateUtil.validNotNull(json, "advanceBookingTime");
        } else {
            ValidateUtil.validNotNull(json, "pickupType");
            validPickupType(json, "pickupType");

            if (json.getString("pickupType").equals(InboundEnumConstant.PickupTypeStatus.VISIT_PICKUP.getVal())) {
                ValidateUtil.validNotNull(json, "date");
                Object dateStr = json.get("date");
                validDate(dateStr, "date");
                // 提货地址
                ValidateUtil.validNotNull(json, "addressCode");
                ValidateUtil.validNotNull(json, "advanceBookingTime");
            } else {
                ValidateUtil.validMaxLength(json, "expressVendorName", "Express Vendor Name", 50);
                // 快递单号
                ValidateUtil.validMaxLength(json, "expressVendorCode", 30);
                ValidateUtil.validMaxLength(json, "expressNo", "Express No", 30, true);
            }
        }
    }

    /**
     * 未提交页面  提交校验
     * 
     * @param json
     */
    public static void validInspectionPickUp(JSONObject json) {
        // 验货方式
        ValidateUtil.validMaxLength(json, "inspectionType", 2, true);
        validInspectionType(json, "inspectionType");
        if (InboundEnumConstant.InspectionTypeStatus.VISIT_INSPECTION.getVal().equals(json.getString("inspectionType"))) {//上门验货
        	throw new ApiException(ErrorCode.NOT_SUPPORT_INSPECTION_VI);
//            ValidateUtil.validNotNull(json, "reservePickupDateFrom");
//            ValidateUtil.validNotNull(json, "reservePickupDateTo");
//            ValidateUtil.validDateString(json, "reservePickupDateFrom", DateUtil.FULL_DATE_STRING);
//            ValidateUtil.validDateString(json, "reservePickupDateTo", DateUtil.FULL_DATE_STRING);
//            validateReservePickupDate(json, "reservePickupDateFrom","reservePickupDateTo");
//            // 验货地址
//            ValidateUtil.validNotNull(json, "pickupAddressCode");
//            ValidateUtil.validMaxLength(json, "pickupAddressCode", 30, true);
        } else if(InboundEnumConstant.InspectionTypeStatus.INSPECTION_WH.getVal().equals(json.getString("inspectionType"))){//验货仓验货
            ValidateUtil.validMaxLength(json, "pickupType", 1, true);
            validPickupType(json, "pickupType");

            if (InboundEnumConstant.PickupTypeStatus.VISIT_PICKUP.getVal().equals(json.getString("pickupType"))) {
                ValidateUtil.validNotNull(json, "reservePickupDateFrom");
                ValidateUtil.validNotNull(json, "reservePickupDateTo");
                ValidateUtil.validDateString(json, "reservePickupDateFrom", DateUtil.FULL_DATE_STRING);
                ValidateUtil.validDateString(json, "reservePickupDateTo", DateUtil.FULL_DATE_STRING);
                validateReservePickupDate(json, "reservePickupDateFrom","reservePickupDateTo");
                // 提货地址
                ValidateUtil.validMaxLength(json, "pickupAddressCode", 30 ,true);
            } else {
                //如果expressVendorCode为空则校验expressVendorName
                if(ValidateUtil.isNullOrEmpty(json.getString("expressVendorCode"))){
                    ValidateUtil.validMaxLength(json, "expressVendorName", "Express Vendor Name", 50, true);
                } else {
                    ValidateUtil.validMaxLength(json, "expressVendorCode", 30);
                }
                // 快递单号
                ValidateUtil.validNotNull(json, "expressNo");
                ValidateUtil.validMaxLength(json, "expressNo", 30, true);
            }
        }else{
        	throw new ApiException(ErrorCode.FIELD_VALUE_NOT_EXISIT, "inspectionType",json.getString("inspectionType"));
        }
    }
    /**
     * 校验预约提货时间是否在同一天
     * @param json
     * @param reservePickupDateFromKey
     * @param reservePickupDateToKey
     */
    private static void validateReservePickupDate(JSONObject json,
			String reservePickupDateFromKey, String reservePickupDateToKey) {
		String from = json.getString(reservePickupDateFromKey).trim();
		String to = json.getString(reservePickupDateToKey).trim();
		String reservePickupDate1 = (from).substring(0, 10);
		String reservePickupDate2 = (to).substring(0, 10);
		if (!reservePickupDate1.equals(reservePickupDate2)) {
			throw new ApiException(ErrorCode.MUST_THE_SAME_DAY,
					reservePickupDateFromKey, reservePickupDateFromKey);
		}
		String time1 = from.substring(from.length() - 8, from.length()).trim();
		String time2 = to.substring(to.length() - 8,to.length()).trim();
		String time1ToTime2 = time1 +"-" + time2;
		boolean isMatch = false;
		StringBuffer sb = new StringBuffer();
		for (String time : PICKUP_DATE_TIMES) {
			sb.append(time).append(",");
			if(time.equals(time1ToTime2)){
				isMatch = true;
			}
		}
		if(!isMatch){
			throw new ApiException(ErrorCode.NOT_SUPPORT_PICKUP_DATE_PERIOD,
					reservePickupDateFromKey, reservePickupDateToKey,sb);
		}

	}
    
    public static void main(String[] args) {
		System.out.println("2016-01-02 11:11:11".substring(0,10).equals("2016-01-02 12:11:11".substring(0,10)));
		System.out.println("2016-01-02 11:11:11".substring(0,10));
		System.out.println(PICKUP_DATE_TIMES.toString());
	}

	/**
     * 验货/提货信息草稿
     * 
     * @param json
     */
    public static void ValidInspectionPickUpByDraftField(JSONObject json) {
        // 入库单
        ValidateUtil.validNotNull(json, "orderNo");
        Object dateStr = json.get("date");
        if (null == dateStr) {
            return;
        }
        validDate(dateStr, "date");
    }

    /**
     * 限制"验货仓验货"必须为VI/WI
     * 
     * @param json
     */
    public static void validInspectionType(JSONObject json, String key) {
    	ValidateUtil.validNotNull(json, "inspectionType");
        String inspectionType = (String) json.get("inspectionType");
        if (!InboundEnumConstant.InspectionTypeStatus.INSPECTION_WH.getVal().equals(inspectionType)
            && !InboundEnumConstant.InspectionTypeStatus.VISIT_INSPECTION.getVal().equals(inspectionType)) {
            throw new ApiException(ErrorCode.FIELD_VALUE_NOT_EXISIT, key, inspectionType);
        }
    }

    /**
     * 限制"自发物流"必须为P/S
     * 
     * @param json
     */
    public static void validPickupType(JSONObject json, String key) {
    	ValidateUtil.validNotNull(json, "pickupType");
        String pickupType = (String) json.get("pickupType");
        if (!InboundEnumConstant.PickupTypeStatus.SPONT_LOGISTICS.getVal().equals(pickupType)
            && !InboundEnumConstant.PickupTypeStatus.VISIT_PICKUP.getVal().equals(pickupType)) {
            throw new ApiException(ErrorCode.FIELD_VALUE_NOT_EXISIT, key, pickupType);
        }
    }

    /**
     * 提货时间格式校验（卖家网站）
     * @param object
     * @param key
     */
    private static void validDate(Object object, String key) {
        String[] dates = object.toString().split("\\|");
        if (dates.length != 2 || StringUtils.isBlank(dates[0]) || StringUtils.isBlank(dates[1])) {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, key);
        }

        Date date = DateUtil.parse2Date(dates[0].trim(), DateUtil.FULL_DATE_STRING);
        String v = DateUtil.parse2String(date, DateUtil.FULL_DATE_STRING);
        String n = DateUtil.parse2String(date, DateUtil.FULL_DATE_STRING1);
        if (!dates[0].trim().toString().equals(v) && !dates[0].trim().toString().equals(n)) {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, key);
        }
        
        date = DateUtil.parse2Date(dates[1].trim(), DateUtil.FULL_DATE_STRING);
        v = DateUtil.parse2String(date, DateUtil.FULL_DATE_STRING);
        String m = DateUtil.parse2String(date, DateUtil.FULL_DATE_STRING1);
        if (!dates[1].trim().toString().equals(v)&&!dates[1].trim().toString().equals(m)) {
            throw new ApiException(ErrorCode.FIELD_TYPE_ERROR, key);
        }
        
    }
    
}
